from PyQt4 import QtGui,QtCore
#from ftllibs.FTL import CHAMPS as CHAMPS
import ftllibs.FTL as FTL

class ChampTab(QtGui.QWidget):
    S_OpenChampBuild = QtCore.pyqtSignal([int])
    def __init__(self):
        super(ChampTab,self).__init__()
        self.MasterLayout = QtGui.QHBoxLayout()
        self.setLayout(self.MasterLayout)
        self.initUI()

    def initUI(self):
        CO=ChampOverview(self)
        CI=ChampInfo(self)
        self.MasterLayout.addWidget(CO)
        self.MasterLayout.addWidget(CI)
        CO.S_ChampDoubleClicked[int].connect(self.EmitOpenBuild)

    def EmitOpenBuild(self,ChampID):
        print "ChampTab -> EmitOpenBuild -> ChampID: ",ChampID
        self.S_OpenChampBuild.emit(ChampID)


class ChampInfo(QtGui.QWidget):
    def __init__(self,parent):
        super(ChampInfo,self).__init__()
        self.parent=parent
        self.setFixedWidth(200)
        self.B_NewBuild = QtGui.QPushButton('New Build')
        self.B_LoadBuilds = QtGui.QPushButton('Load Builds')
        self.L_NumOfBuilds = QtGui.QLabel('Build Count: 0')
        self.ChampPix = QtGui.QPixmap(200,400)
        self.L_ChampPix= QtGui.QLabel()
        self.L_ChampPix.setPixmap(self.ChampPix)
        self.MasterLayout = QtGui.QVBoxLayout()
        self.MasterLayout.addWidget(self.L_ChampPix)
        self.MasterLayout.addWidget(self.B_NewBuild)
        self.MasterLayout.addWidget(self.B_LoadBuilds)
        self.setLayout(self.MasterLayout)
        

class ChampOverview(QtGui.QWidget):
    S_ChampSelected = QtCore.pyqtSignal([int])
    S_ChampDoubleClicked = QtCore.pyqtSignal([int])
    # TODO: Scroll Area + wenn resized wird muss die breite ( X ) neu berechnetwerden, an der breite von self.rect und hintergrund und max_x wert neu gezeichnet,berechnet
    def __init__(self,parent):
        super(ChampOverview,self).__init__()
        self.parent = parent
        self.setMouseTracking(True)
        self.ChampList = FTL.CHAMPS.GetChampList()
        self.champcount = len(self.ChampList)
        self.highlight_x = 0
        self.highlight_y = 0
        self.Pen=QtGui.QPen()
        self.Pen.setColor(QtGui.QColor("#AA0000"))
        self.Pen.setWidth(3)
#        self.BG = QtGui.QPixmap(1,1)

    def BGImage(self):
        y=0
        x=0
        x_off=10
        y_off=10
        if self.rect().size().height() == 0 or self.rect().size().width() == 0:
            return
        img = QtGui.QPixmap(self.rect().size())
        img.fill(QtCore.Qt.transparent)
        p = QtGui.QPainter(img)
        for i in self.ChampList:
            if x > 5:
                x=0
                y+=1
            p.drawPixmap(90*x+x_off,90*y+y_off,QtGui.QPixmap("images/champs/small/%s" % (FTL.CHAMPS.CHAMPS[str(FTL.CHAMPS.GetChampID(i))].IconSmall)))
            x += 1
        p.end()
        self.BG=img

    def paintEvent(self,e):
        x_off=10
        y_off=10
        p = QtGui.QPainter(self)
        p.setPen(self.Pen)
        HighRect = QtCore.QRect()
        HighRect.setRect(x_off+90*(self.highlight_x),y_off+90*(self.highlight_y),80,80)
        p.drawPixmap(0,0,self.BG)
        if (self.highlight_y*6 + self.highlight_x) > self.champcount-1:
            pass
        else:
            p.drawRect(HighRect)

    def resizeEvent(self,e):
        self.BGImage()

    def mouseMoveEvent(self,e):
        x = e.pos().x()
        y = e.pos().y()
        x= int(x/90.0)
        y= int(y/90.0)
        if x != self.highlight_x or y != self.highlight_y:
            self.highlight_x = x
            self.highlight_y = y
            self.update()

    def mousePressEvent(self,e):
        temp = self.highlight_y*6 + self.highlight_x
        if not temp > len(self.ChampList):
            ChampID=FTL.CHAMPS.GetChampID(self.ChampList[temp])
            print "Emit -> S_ChampSelected -> ",ChampID
            self.S_ChampSelected.emit(ChampID)

    def mouseDoubleClickEvent(self,e):
        temp = self.highlight_y*6 + self.highlight_x
        if not temp > len(self.ChampList):
            ChampID=FTL.CHAMPS.GetChampID(self.ChampList[temp])
            print "Emit -> S_ChampDoubleClicked -> ",ChampID
            self.S_ChampDoubleClicked.emit(ChampID)
